/*
Copyright 2014 Aaron Batilo

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

//@author: Aaron Batilo

//This class represents a file on the disk

#include "File.h"

string myName;
int myType;

File::File()
{

}

File::File(string inName)
{
	myName = inName;
	detectExtension();
}

File::File(string inName, int inType)
{
	myName = inName;
	myType = inType;
}

File::~File()
{
}

//Gets the extension of the created file, if the file extension is recognized as a cmp or a txt, set myType to correspond
void File::detectExtension()
{
	//Split the name of the file from its extension
	vector<string> fileNameSplit = StringUtility::split(myName, '.');

	//Ensure that the file split yielded both a name and an extension
	if (fileNameSplit.size() > 1)
	{
		//Ensure the file extension is lowercase for easier comparison
		string lowercaseFileExtension = StringUtility::toLower(fileNameSplit[1]);

		//Check to see if the file is a cmp file
		if (lowercaseFileExtension == "cmp")
		{
			myType = FileType::CMP;
		}
		//Check to see if the file is a txt
		else if (lowercaseFileExtension == "txt")
		{
			myType = FileType::TXT;
		}
	}
}

//Reads the content of the file
string File::readContents()
{
	//The empty contents to return
	string contents = "";

	//Ensure the file exists
	if (isExists())
	{
		//This represents the current line from the file
		string currentLine;
		//Create an input file stream to the file
		ifstream myFile(myName.c_str());

		//Ensure the stream is valid
		if (myFile.is_open())
		{
			//Loop through while there is still a valid line from the file
			while (getline(myFile, currentLine))
			{
				//add these contents to the return variable
				contents += currentLine + "\n";
			}

			//Remove the trailing new line terminator
			contents = contents.substr(0, contents.size() - 1);

			//Once done close the stream to the file
			myFile.close();
		}
	}

	//Return the contents of the file
	return contents;
}

//Return the name of the file
string File::getName()
{
	return myName;
}

string File::getNameNoExtension()
{
	return StringUtility::split(myName, '.')[0];
}

//Get the extension type according to the FileType struct
int File::getExtension()
{
	return myType;
}

//Return the extension as a string
string File::getExtensionString()
{
	if (myType == FileType::CMP)
		return "cmp";
	else if (myType == FileType::TXT)
		return "txt";
	else
		return "null";
}

//Check to see if the file created exists
bool File::isExists()
{
	//Try to get an input stream to the file
	ifstream myFile(myName.c_str());

	//If the stream is valid, we know the file exists
	if (myFile.good())
	{
		myFile.close();
		return true;
	}
	//Otherwise, the file doesn't exist
	else
	{
		myFile.close();
		return false;
	}
}
